//
//  LoginAndRegisterViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/13.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LoginAndRegisterViewController.h"
#import "RegisterViewController.h"
#import "LabelAndTextView.h"
#import "BaseTabBarController.h"
#import "AppDelegate.h"



@interface LoginAndRegisterViewController ()

@property(nonatomic,retain)BaseLabel *welcomeLabel;

@property(nonatomic,retain)LabelAndTextView *nameView;
@property(nonatomic,retain)LabelAndTextView *passwordView;

@property(nonatomic,retain)BaseButton *registerButton;
@property(nonatomic,retain)BaseButton *loginButton;



@end

@implementation LoginAndRegisterViewController

-(void)dealloc{
    [_welcomeLabel release];
    [_nameView release];
    [_passwordView release];
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    BaseImageView *bachatImageV = [[BaseImageView alloc]init];
    [self.view addSubview:bachatImageV];
    bachatImageV.layer.masksToBounds = YES;
    bachatImageV.layer.cornerRadius = 5;
    bachatImageV.image = [UIImage imageNamed:@"Logo"];
    [bachatImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(ROOTWIDTH / 2 - 40);
        make.width.height.mas_equalTo(80);
        make.top.mas_equalTo(80);
        
    }];
    [bachatImageV release];
    
    
    
    self.welcomeLabel = [[BaseLabel alloc]init];
    self.welcomeLabel.backgroundColor = [UIColor whiteColor];
    self.welcomeLabel.text = @"使用帐号和密码登录";
    self.welcomeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.welcomeLabel];
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(bachatImageV.mas_bottom).offset(20);
        
    }];
    
    
    [_welcomeLabel release];
    
  
    self.nameView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(ROOTWIDTH / 15,240, ROOTWIDTH / 4 * 3 + 30, 40)];
    self.nameView.mylabel.text = @"帐号";
    self.nameView.myTextField.placeholder = @"    用户名/手机号";
    [self.view addSubview:self.nameView];
    [_nameView release];
    
    self.passwordView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(self.nameView.frame.origin.x, self.nameView.frame.origin.y + self.nameView.frame.size.height + 20, self.nameView.frame.size.width, self.nameView.frame.size.height)];
    self.passwordView.mylabel.text = @"密码";
    self.passwordView.myTextField.placeholder = @"    请填写密码";
    self.passwordView.myTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordView];
    [_passwordView release];
    
    
    
    BaseButton *loginButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:loginButton];
    
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(self.passwordView.mas_bottom).offset(20);
        make.height.mas_equalTo(40);
        
    }];
    
    loginButton.backgroundColor = RGBAColor(205, 201, 201);
    [loginButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.layer.masksToBounds = YES;
    loginButton.layer.cornerRadius = 5;
    
    self.loginButton = loginButton;
    
    //取消图片Button的点击效果
    loginButton.adjustsImageWhenHighlighted = NO;
    [loginButton addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.registerButton = [BaseButton buttonWithType:UIButtonTypeCustom];
  [self.view addSubview:self.registerButton];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.bottom.mas_equalTo(-30);
        make.height.mas_equalTo(40);
        
    }];
    self.registerButton.backgroundColor = [UIColor whiteColor];
    self.registerButton.layer.borderWidth = 1;
    self.registerButton.layer.borderColor = [[UIColor grayColor] CGColor];
    self.registerButton.layer.masksToBounds = YES;
    self.registerButton.layer.cornerRadius = 5;
    [self.registerButton setTitle:@"没有帐号? 请注册" forState:UIControlStateNormal];
    self.registerButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.registerButton setTitleColor:RGBAColor(70, 130, 180) forState:UIControlStateNormal];
   
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
//监听输入内容的情况
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonSelected:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark -- textFiel改变时通知button改变样式
-(void)buttonSelected:(NSNotification *)noti{
    
    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;

    
    //任意一个为空,注册按钮都是暗的没有点击效果
    if (name.length == 0 || password.length == 0 ) {
        
        self.loginButton.userInteractionEnabled = NO;
        self.loginButton.backgroundColor = RGBAColor(205, 201, 201);
        [self.loginButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];


    }else{
    //都不为空,注册按钮变亮可以注册
        self.loginButton.userInteractionEnabled = YES;
        
        self.loginButton.backgroundColor = RGBAColor(138, 43, 226);
        [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    
    
    
    
}



#pragma mark -- 登录按钮
-(void)loginButton:(BaseButton *)button{
    
    [self.view endEditing:YES];
    
    MBProgressHUD *hudLogin = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hudLogin.labelText = @"正在登录";
    

    
    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;
    
   [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:name password:password completion:^(NSDictionary *loginInfo, EMError *error) {
       if (!error) {
           NSLog(@"登录成功");
           [hudLogin hide:YES];
           [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userName"];
           
           //设置为自动登录
           [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
           
           BaseTabBarController *tabBarVC = [[BaseTabBarController alloc]init];
           
           AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
           
           app.window.rootViewController = tabBarVC;
           
       }else{
//登录失败,只能有一个原因是 帐号或密码错误
           [hudLogin hide:YES];
           MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
           hud.mode = MBProgressHUDModeCustomView;
           hud.labelText = @"帐号或密码错误";
           [hud hide:YES afterDelay:1];
           NSLog(@"%@",loginInfo);
           
           
       }
       
       
       
       
   } onQueue:dispatch_get_main_queue()];

    
    
    
    
}





#pragma mark -- 注册按钮
-(void)registerButtonAction:(BaseButton *)button{
    
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    UINavigationController *registerNaVC = [[UINavigationController alloc]initWithRootViewController:registerVC];
    registerVC.passValue = ^(NSString *name, NSString *password){
        
        self.nameView.myTextField.text = name;
        self.passwordView.myTextField.text = password;
        
        
    };
    
    [registerVC release];
    [self presentViewController:registerNaVC animated:YES completion:^{
        
        
    }];
    [registerNaVC release];
    
    
    
    
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.nameView.myTextField resignFirstResponder];
    [self.passwordView.myTextField resignFirstResponder];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
